<template>
    <div>
        <el-card class="box-card" shadow="never">
            <!--导航栏-->
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item>直播群</el-breadcrumb-item>
                <el-breadcrumb-item>直播群列表</el-breadcrumb-item>
            </el-breadcrumb>
            <!--分割线-->
            <div class="yx_line"></div>
            <el-row :gutter="10" style="margin-top:20px;">
               <el-col :span='5'>
                  <el-input v-model="name">
                    <template slot="prepend">群名称</template>
                  </el-input>
               </el-col>
               <el-col :span='5'>
                    <el-select v-model="status" placeholder="状态" clearable>
                        <el-option
                                v-for="item in allstatus"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
               </el-col>
               <el-col :span="5">
                 <el-date-picker
                       v-model="start_date"
                       type="date"
                       value-format="yyyy-MM-dd"
                       placeholder="起始日期" style="width:100%">
                     </el-date-picker>
                </el-col>
                <el-col :span="5">
                      <el-date-picker
                        v-model="end_date"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="截止日期" style="width:100%">
                      </el-date-picker>
                 </el-col>
            </el-row>
            <el-row class="yx_el_row">
                <el-col>
                    <el-button type="success" plain @click="search">筛选</el-button>
                </el-col>
            </el-row>
            <el-table class="yx_table" border :data="grouplist">
                <el-table-column label="序号" width="100">
                     <template slot-scope="scope">{{(scope.$index+1)+(current_page-1)*20}}</template>
                </el-table-column>
                <el-table-column prop="name" label="群名称" width="100">
                </el-table-column>
                <el-table-column prop="creator_name" label="创建者" width="80">
                </el-table-column>
                <el-table-column prop="start_time" label="直播开始时间" width="150">
                </el-table-column>
                <el-table-column prop="expire" label="有效天数" width="100">
                </el-table-column>
                <el-table-column prop="status" label="状态" width="100">
                </el-table-column>
                <el-table-column prop="create_time" label="创建时间" width="150">
                </el-table-column>
                <el-table-column prop="total_user_count" label="总用户数" width="100">
                </el-table-column>
                <el-table-column  label="操作" width="100">
                   <template slot-scope="scope">
                       <el-tooltip class="item" effect="dark" content="排行榜" placement="bottom-start">
                           <el-button type="warning" icon="el-icon-s-data" @click="rank(scope.row.id,scope.row.name)" circle></el-button>
                       </el-tooltip>
                   </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注">
                </el-table-column>
            </el-table>
            <el-pagination class="yx_page"
                           background
                           @current-change="handle_current_change"
                           @size-change="handle_size_change"
                           :current-page="current_page"
                           :page-sizes="page_sizes"
                           :page-size="page_size"
                           layout="prev, pager, next,sizes, total"
                           :total="total_count">
            </el-pagination>
        </el-card>
        <el-dialog title="排行榜" :visible.sync="showRank" width="70%" destroy-on-close>
          <DataCenter ref="dc" :option="option" :show_loading="show_loading"></DataCenter>
        </el-dialog>
    </div>
</template>

<script>
    import DataCenter from '../datacenter/DataCenter.vue';
    export default {
        name: 'GroupList',
        inject:['reload'],
        components:{ DataCenter },
        data() {
            return {
                current_page:1,
                page_size:20,
                showDialog:false,
                loading:false,
                allstatus:[
                   { label:'待开始',value:'1'},
                   { label:'进行中',value:'2'},
                   { label:'已解散',value:'3'}
                ],
                name:'',
                status:'',
                start_date:'',
                end_date:'',
                showRank:false,
                //下面是柱形图option,show_loading表示是否展示加载图标
                show_loading:false,
                option:{
                    title: {
                        text: '用户分享量前10名',
                        textStyle:{
                           fontWeight:'normal'
                        }
                    },
                    tooltip: {},
                    xAxis: {
                        name:'昵称',
                        axisLabel:{
                           interval: 0,
                        },
                        data:[]
                    },
                    yAxis: {
                        name:'单位：人数'
                    },
                    series: [{
                        name: '分享用户数',
                        type: 'bar',
                        data:[]
                    }]
                }
            }
        },
        methods: {
            getList(){
                var send_data={
                    api_token:this.$store.getters.getSessionId,
                    current_page:this.current_page,
                    page_size:this.page_size,
                    name:this.name,
                    status:this.status==''?-1:this.status,
                    start_date:this.start_date,
                    end_date:this.end_date
                }
                this.$store.dispatch('loadGroupList',send_data);
            },
            //跳转页
            handle_current_change(current_page){
                //当前页码数
                this.current_page=current_page;
                this.getList();
            },
            //修改条数
            handle_size_change(page_size){
                this.current_page=1;
                this.page_size=page_size;
                this.getList();
            },
            search(){
               this.current_page=1;
               this.getList();
            },
            rank(group_id,d_name){
               //先清空option中的data
               this.option.xAxis.data=[];
               this.option.series[0].data=[];
               this.option.title.text=d_name+'用户分享量前10名';
               this.show_loading=true;

               let send_data={
                  api_token:this.$store.getters.getSessionId,
                  group_id:group_id
               }
               this.$store.dispatch('loadRankList',send_data);
            }
        },
        created() {
            //加载列表
            this.getList();
        },
        computed:{
            grouplist(){
                return this.$store.getters.getGroupList;
            },
            total_count(){
                return this.$store.getters.getGroupTotalCount;
            },
            page_sizes(){
                return this.$store.state.page_sizes;
            },
            ranklist(){
               return this.$store.getters.getRankList;
            }
        },
        watch:{
            //排行榜
            ranklist(val){
                if(val.length>0){
                   this.showRank=true;
                   let name_arr=[];
                   let val_arr=[];
                   for(let i in val){
                       name_arr.push(val[i]['nickname']);
                       val_arr.push(val[i]['shareTotalCount']);
                   }
                   this.option.xAxis.data=name_arr;
                   this.option.series[0].data=val_arr;
                   let that=this;
                   setTimeout(function(){
                     that.show_loading=false;
                     that.$refs.dc.setChartOption(that.option);
                   },1000)
                }else{
                   this.$message({
                       message:'暂无数据',
                       type:'error',
                       duration:2000,
                   });
                }
            },
            grouplist(val){
               if(val.length>0){
                  for(let i in val){
                     if(val[i]['status']==1){
                        val[i]['status']='待开始';
                     }else if(val[i]['status']==2){
                        val[i]['status']='进行中';
                     }else{
                        val[i]['status']='已解散';
                     }
                  }
               }
            }
        }
    }
</script>

<style>
     @import url('../../assets/css/table.css');
</style>
